-- DragonKingBox
-- Created by chengb Aug/5/2016
-- 龙王城宝箱怪格子

-- 初始化
function initDragonKingBox(self)
    self.item:removeAllChildren();

    local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);
    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. self.gridData.class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 开启宝箱
            local function okFunc()
                if DungeonActionM.go("open_dragon_king_box", self.gridData:getPos(), 0) then
                    alert(getLocStr("open_dragon_king_box_tip"));
                end
            end

            -- 离开
            local function cancelFunc()
                DungeonActionM.go("open_dragon_king_box", self.gridData:getPos(), 1);
            end

            self:openBuildingView2(okFunc, cancelFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 宝箱已被开启，格子需要隐藏
    if BraveVSDragonM.hasDragonKingBoxOpend(self.gridData.class) then
        self.icon:setVisible(false);
    else
        self.icon:setVisible(true);
    end
end
